{include file="public/head"}
<style>
    .caozuo .layui-btn {
        margin-left: auto;
    }

    .x-body {
        min-width: 1300px;
    }

    body {
        overflow-x: scroll;
    }
    .layui-layer-btn{
        background-color: #F0F0F0;
    }
</style>
<body>
<div class="x-body">
    <div class="lotus-nav">

        <div class="layui-input-inline">
            <input id="proname" name="proname" class="layui-input" placeholder="产品名称" value="{$Think.get.proname}">
        </div>

        <div class="layui-input-inline">
            <input id="code" name="code" class="layui-input" placeholder="SKU码" value="{$Think.get.code}">
        </div>

        <div class="layui-input-inline layui-form">
            <select lay-filter="categoryname" name="bcate_id" id='bcate_id' lay-search>
                <option value="">商品分类</option>
                {volist name='category' id='vo'}
                <option value="{$vo.id}" title="{$vo.platform}" data-name="{$vo.bname}" {if $Think.get.bcate_id eq $vo['id']} selected {/if}>{$vo.bname}--{$vo.platform}</option>
                {/volist}
            </select>
            <input type="hidden" id="bplatform" value="{$Think.get.platformStr}">
            <input type="hidden" id="bname" name="bname" value="{$Think.get.bname}">
        </div>

        <div class="layui-input-inline layui-form" style="width: 150px">
            <select name="platform" id="platform" lay-filter="platform">
                <option value="">商品平台</option>
                {volist name="platform" id="vo"}
                <option value="{$vo.english}" {if $Think.get.platformStr eq $vo['english']} selected {/if}>{$vo.name}</option>
                {/volist}
            </select>
        </div>

        <div class="layui-input-inline layui-form" style="width: 150px">
            <select name="audit" id="audit">
                <option value="">审核状态</option>
                {foreach name="audits" item="vo" key="key" }
                {if $key eq $Think.get.audit}
                <option value="{$key}" selected>{$vo}</option>
                {else / }
                <option value="{$key}">{$vo}</option>
                {/if}
                {/foreach}
            </select>
        </div>

        <div class="layui-input-inline layui-form" style="width: 150px">
            <select name="lang" id="lang" lay-search>
                <option value="">默认语言</option>
                {volist name="lang" id="vo"}
                <option value="{$vo.lanid}" {if $Think.get.lanid eq $vo['lanid']} selected {/if}>{$vo.lang}</option>
                {/volist}
            </select>
        </div>

        <div class="layui-input-inline" style="width: 100px">
            <input id="start" name="start" class="layui-input" placeholder="开始日期" value="{$Think.get.start}">
        </div>

        <div class="layui-input-inline" style="width: 100px">
            <input id="end" name="end" class="layui-input" placeholder="结束日期" value="{$Think.get.end}">
        </div>

        <input type="hidden" id="company_id" name="company_id" value="{$Think.get.company_id}">

        <div class="layui-input-inline layui-form" style="width: 150px">
            <select name="list_rows" id="list_rows">
                <option value="">每页产品数</option>
                {volist name="list_rows" id="vo"}
                {if $vo eq $Think.get.list_rows}
                <option value="{$vo}" selected>{$vo}</option>
                {else / }
                <option value="{$vo}">{$vo}</option>
                {/if}
                {/volist}
            </select>
        </div>
        <button class="layui-btn layui-btn-sm" onclick="search()">
            <i class="layui-icon layui-icon-search"></i>搜索
        </button>
        {if empty($Think.get.company_id)}
        <br><br>
        <button class="layui-btn layui-btn-sm"
                onclick="x_admin_show('添加产品','productAdd.html?lang={$Think.get.lanid}&company_id={$Think.get.company_id}')">
            <i class="layui-icon layui-icon-add-circle"></i>新建
        </button>

        <button class="layui-btn layui-btn-sm layui-btn-danger" onclick="delAll('{$Think.get.lanid}')"><i
                class="layui-icon"></i>批量删除
        </button>
        <button class="layui-btn layui-btn-sm layui-btn-normal" onclick="massAudit('{$Think.get.lanid}')">批量审核</button>

        <button class="layui-btn layui-btn-sm" onclick="uploadProduct()">批量导入</button>
        <a href="/template/ERP后台多变体产品导入模板.csv" class="layui-btn layui-btn-sm">
            <i class="layui-icon">&#xe656;</i>点击下载批量导入模板
        </a>

        <button class="layui-btn layui-btn-sm layui-btn-warm" id="tipexcel" onclick="exportExcel()">导出Excel</button>

        <button class="layui-btn layui-btn-sm layui-btn-normal" onclick="massTransfer('{$Think.get.lanid}')">批量转移</button>

        <button class="layui-btn layui-btn-sm" id="tipchange" onclick="changePrice('{$Think.get.lanid}')">批量改价
        </button>

        <button class="layui-btn layui-btn-sm" id="tipsynch" onclick="synchPrice('{$Think.get.lanid}')">价格同步
        </button>

        <button class="layui-btn layui-btn-sm layui-btn-normal" onclick="translateAll('{$Think.get.lanid}')")>
            批量翻译
        </button>

        <button type="button" style="display: none" id="upload_file">批量导入</button>
        {/if}

        <span class="x-right">共有数据:<i class="i_count">{$product->total()}</i>条
            <button onclick="javascript:location.reload()" class="layui-btn-default ">
                <i class="layui-icon layui-icon-refresh"></i>
            </button>
        </span>
    </div>


    <table class="layui-table">
        <thead>
        <tr>
            <th style="width: 55px;text-align:center">
                <div class="header layui-form-checkbox" lay-skin="primary"><i class="layui-icon">&#xe605;</i></div>
            </th>
            <th style="width:100px;text-align:center">产品图</th>
            <th>产品标题</th>
            <th style="width: 80px;text-align:center">售价</th>
            <th style="min-width: 80px;text-align:center">SKU码</th>
            <th style="width: 60px;text-align:center">分类</th>
            <th style="width: 80px;text-align:center">审核状态</th>
            <th style="width: 60px;text-align:center">业务员</th>
            <th style="width: 60px;text-align:center">智赢状态</th>
            <th style="width: 75px;text-align:center">加入时间</th>
            {if isession('company_id') == $Think.get.company_id || $Think.get.company_id ==''}
            {if $Think.get.lanid == ''}
            <th style="width: 260px;text-align:center">操作</th>
            {else/}
            <th style="width: 80px;text-align:center">操作</th>
            {/if}
            {else/}
            {if empty($Think.get.company_id)}
            <th style="width: 200px;text-align:center">操作</th>
            {else/}
            <th style="width: 40px;text-align:center">操作</th>
            {/if}
            {/if}
        </thead>
        <tbody>

        {volist name="product" id="vo"}
        {if $vo.repeat eq 1}
        <tr class="layui-bg-orange">
        {else/}
        <tr>
        {/if}
            <td style="text-align:center">
                <div class="layui-form-checkbox" lay-skin="primary" data-id='{$vo.guid}'><i
                        class="layui-icon">&#xe605;</i></div>{$i}
            </td>
            <td style="text-align:center">
                {if isset($imgs[$vo['guid']]['imgpath'])}
                <img src="{$imgs[$vo['guid']]['imgpath']}" onclick="previewImg(this)">
                {else/}
                <img src="__images__/noimg.png">
                {/if}
            </td>
            <td>
                {if !empty($vo['fromurl'])}
                <a href="{$vo.fromurl}" style="color: #01AAED;" target="_blank">{$vo.proname}</a>
                {else/}
                {$vo.proname}
                {/if}
            </td>
            <td style="text-align:center">{$vo.coin}{$vo.saleprice}</td>
            <td style="text-align:center">{$vo.skucode}</td>
            <td style="text-align:center">{$vo.bname}</td>
            <td style="text-align:center">
                {if $vo['audit'] == 1}未审核
                {elseif $vo['audit'] == 2}<span style="color: green"><b>已审核</b></span>
                {else/}<span style="color: red"><b>产品侵权</b></span>
                {/if}
            </td>
            <td style="text-align:center">{$vo.user_name}</td>
            <td style="text-align:center">
                {if $vo['iszy'] == 1}未上传
                {else/}已上传
                {/if}
            </td>
            <td style="text-align:center">{$vo.ndate}</td>
            <td class="caozuo">
                {if ssession('username') eq $vo.user_name}
                <button class="layui-btn layui-btn-xs"
                        onclick="x_admin_show('编辑','productEdit.html?id={$vo.id}&lang={$Think.get.lanid}&company_id={$Think.get.company_id}')">
                    编辑
                </button>
                {else/}
                <button class="layui-btn layui-btn-xs"
                        onclick="x_admin_show('详情','productInfo.html?id={$vo.id}&lang={$Think.get.lanid}&company_id={$Think.get.company_id}')">
                    详情
                </button>
                {/if}

                {if empty($Think.get.company_id)}
                <button onclick="del(this,'{$vo.guid}','{$Think.get.lanid}')"
                        class="layui-btn layui-btn-xs layui-btn-danger">
                    删除
                </button>

                    {if $vo['audit'] == 1}
                    <button onclick="audit(this,'{$vo.guid}','{$Think.get.lanid}',2,'{$vo.id}')"
                            class="layui-btn layui-btn-xs layui-btn-normal">
                        审核
                    </button>

                    <button onclick="audit(this,'{$vo.guid}','{$Think.get.lanid}',3,'{$vo.id}')"
                                class="layui-btn layui-btn-xs layui-bg-cyan">
                        侵权
                    </button>
                    {elseif ($vo['audit'] == 2) && ($Think.get.lanid == '')}
                        {if ssession('username') eq $vo.user_name}
                        <button class="layui-btn layui-btn-xs layui-btn-normal"
                                onclick=(window.external.Translate('{$Think.get.lanid}','{$vo.guid}'))>
                            一键翻译
                        </button>
                        {/if}
                        {if isession('company_id') == $Think.get.company_id || $Think.get.company_id ==''}
                            {if isession('type') ==1}
                            <button class="layui-btn layui-btn-xs layui-btn-normal"
                                    onclick="shares('{$vo.id}','{$Think.get.lanid}')">一键分享
                            </button>
                            {else/}
                            <button class="layui-btn layui-btn-xs layui-btn-normal"
                                    onclick="share('{$vo.id}','{$Think.get.lanid}')">一键分享
                            </button>
                            {/if}
                        {/if}
                        {if $vo['iszy'] == 1}
                        <br>
                        <button onclick="updatezy(this,'{$Think.get.lanid}','{$vo.id}')"
                                class="layui-btn layui-btn-xs layui-btn-primary">
                            上传智赢
                        </button>
                        {/if}
                    {/if}
                    {if ssession('username') eq $vo.user_name}
                    <button onclick="repeat('{$vo.guid}','{$Think.get.lanid}')"
                            class="layui-btn layui-btn-xs" style="background-color: #5FB878">
                        重复检测
                    </button>
                    {/if}
                {/if}
            </td>
        </tr>
        {/volist}

        </tbody>
    </table>
    <div class="page">
        <div>
            {$product->render()}
        </div>
    </div>
</div>
<input type="hidden" id="guid" value="">
</body>
{include file="public/foot"}
<script>
    window.external=formProcess;
    var layer = layui.layer;
    var form = layui.form;
    var upload = layui.upload;
    var laydate = layui.laydate;

    var tip_index = 0;
    $(document).on('mouseenter', '#tipexcel', function(){
        tip_index = layer.tips("审核状态、语言、日期为通用可选导出条件", "#tipexcel");
    }).on('mouseleave', '#tipexcel', function(){
        layer.close(tip_index);
    });

    $(document).on('mouseenter', '#tipchange', function(){
        tip_index = layer.tips("审核状态、语言、日期为通用可选批量改价条件", "#tipchange");
    }).on('mouseleave', '#tipchange', function(){
        layer.close(tip_index);
    });

    $(document).on('mouseenter', '#tipsynch', function(){
        tip_index = layer.tips("审核状态、语言、日期为通用可选价格同步条件", "#tipsynch");
    }).on('mouseleave', '#tipsynch', function(){
        layer.close(tip_index);
    });

    laydate.render({
        elem: '#start'
    });

    laydate.render({
        elem: '#end'
    });

    form.on('select(categoryname)', function (data) {
        $("#bplatform").val(data.elem[data.elem.selectedIndex].title);
        $("#bname").val(data.elem[data.elem.selectedIndex].getAttribute("data-name"));
    });
    function search() {
        var html = '';
        html += "&list_rows=" + $("#list_rows").val();
        html += "&start=" + $("#start").val();
        html += "&end=" + $("#end").val();
        html += "&proname=" + $("#proname").val();
        html += "&audit=" + $("#audit").val();
        html += "&code=" + $("#code").val();
        html += "&lanid=" + $("#lang").val();
        html += "&company_id=" + $("#company_id").val();
        var bcate_id = $("#bcate_id").val();
        html += "&bcate_id=" + bcate_id;
        html += "&bname=" + $("#bname").val();
        if(bcate_id){
            html += "&platformStr=" + $("#bplatform").val();
        }else{
            html += "&platformStr=" + $("#platform").val();
        }
        location.href = "productListManual.html?" + html;
    }

    function shares(id, lang) {
        layer.confirm('选择分享范围', {
            btn: ['公共', '私有'] //按钮
        }, function () {
            productshare(id, lang, 2);
        }, function () {
            productshare(id, lang, 1);
        });
    }

    function share(id, lang) {
        layer.confirm('确定分享当前公司？', {
            btn: ['确定', '取消'] //按钮
        }, function () {
            productshare(id, lang, 1);
        });
    }

    function productshare(id, lang, type) {
        layer.load(1);
        $.post("{:url('admin/product/productShare')}", {id: id, lang: lang, type: type}, function (data) {
            layer.closeAll('loading');
            if (data.code == 1) {
                layer.alert(data.msg, {icon: 1},function(){
                    location.reload();
                });
            } else {
                layer.msg(data.msg, {icon: 2, time: 1000});
            }
        });
    }
    
    function uploadProduct() {
        var platform = $("#platform").val();
        if(platform == ''){
            layer.msg('请先选择上传平台');
            return false;
        }
        $("#upload_file").click();
    }

    //执行实例
    upload.render({
        elem: '#upload_file' //绑定元素
        , url: 'import.html' //上传接口
        , data :{
            platform:function(){
                return $('#platform').val();
            }
        }
        , accept: 'file'
        , before: function (obj) { //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
            layer.load(0, {shade: [0.3, '#000']}); //上传loading
        },done: function(data){
            if (data.code == 0) {
                layer.msg(data.msg);
                layer.closeAll('loading');
            } else {
                layer.msg(data.msg, {icon: 1, time: 500}, function () {
                    location.reload();
                    layer.closeAll('loading');
                });
            }
        }
        ,error: function(){
            //请求异常回调
        }
    });

    //删除所有
    function delAll(lang) {
        var data = tableCheck.getData();
        if (data.length == 0) {
            layer.msg("请选择需要删除的产品");
            return false;
        }
        layer.confirm('确认要全部删除吗？', function (index) {
            //捉到所有被选中的，发异步进行删除
            $.post("{:url('admin/product/delete')}", {guid: data, lang: lang}, function (data) {
                console.log(data);
                if (data.code == 1) {
                    layer.msg(data.msg, {icon: 1, time: 1000}, function () {
                        $(".layui-form-checked").not('.header').parents('tr').remove();
                    });
                } else {
                    layer.msg(data.msg, {icon: 2, time: 1000});
                }

            });
        });
    }

    function del(obj, guid, lang) {
        var count = $(".i_count")[0].innerText;
        layer.confirm('确认要删除吗？', function (index) {
            //捉到所有被选中的，发异步进行删除
            $.post("{:url('admin/product/delete')}", {guid: guid, lang: lang}, function (data, textStatus, xhr) {
                /*optional stuff to do after success */
                console.log(data);
                if (data.code == 1) {
                    layer.msg(data.msg, {icon: 1, time: 1000}, function () {
                        $(obj).parents('tr').remove();
                        $(".i_count").html(count - 1);
                    });
                } else {
                    layer.msg(data.msg, {icon: 2, time: 1000});
                }

            });
        });
    }

    function audit(obj, guid, lang, type, id) {
        var info = type == 2 ? "审核通过" : "侵权";
        layer.confirm('确认该产品' + info + '？', function (index) {
            layer.load(1);
            $.post("{:url('admin/product/audit')}", {
                guid: guid,
                lang: lang,
                audit: type,
                id: id
            }, function (data) {
                layer.closeAll('loading');
                if (data.code == 1) {
                    layer.alert(data.msg, {icon: 1},function(){
                        location.reload();
                    });
                } else {
                    layer.msg(data.msg, {icon: 2, time: 1000});
                }

            });
        });
    }
    
    //批量审核
    function massAudit(lang) {
        var data = tableCheck.getData();
        if (data.length == 0) {
            layer.msg("请选择需要审核的产品");
            return false;
        }
        layer.confirm('确认要全部审核通过吗？', function (index) {
            layer.load(1);
            //捉到所有被选中的，发异步进行删除
            $.post("{:url('admin/product/audit')}", {guid: data, lang: lang}, function (data) {
                layer.closeAll('loading');
                if (data.code == 1) {
                    layer.msg(data.msg, {icon: 1, time: 1000}, function () {
                        location.reload();
                    });
                } else {
                    layer.msg(data.msg, {icon: 2, time: 1000});
                }

            });
        });
    }

    function updatezy(obj, lang, id) {
        layer.confirm('确认将该产品上传智赢？', function (index) {
            layer.load(1);
            $.post("{:url('admin/product/updatezy')}", {
                lang: lang,
                id: id
            }, function (data) {
                layer.closeAll('loading');
                if (data.code == 1) {
                    layer.alert(data.msg, {icon: 1},function(){
                        location.reload();
                    });
                } else {
                    layer.alert(data.msg, {icon: 2, time: 1000});
                }

            });
        });
    }

    //批量转移
    function massTransfer(lang) {
        var data = tableCheck.getData();
        if (data.length == 0) {
            layer.msg("请选择需要转移的产品");
            return false;
        }
        var bcate_id = $("#bcate_id").val();
        if(bcate_id == ''){
            layer.msg("请选择需要转移到的分类");
            return false;
        }
        layer.confirm('只能转移当前用户下的产品，确认要全部转移吗？', function (index) {
            //捉到所有被选中的，发异步进行删除
            $.post("{:url('admin/product/massTransfer')}", {
                guid: data,
                bname:$("#bname").val(),
                platform:$("#bplatform").val(),
                lang:lang
            }, function (data) {
                if (data.code == 1) {
                    layer.msg(data.msg, {icon: 1, time: 1000}, function () {
                        location.reload();
                    });
                } else {
                    layer.msg(data.msg, {icon: 2, time: 1000});
                }
            });
        });
    }

    //批量导出
    function exportExcel() {
        var start = $("#start").val();
        var end = $("#end").val();
        var lanid = $("#lang").val();
        var audit = $("#audit").val();
        var data = tableCheck.getData();
        if (data.length != 0) {
            export_show(data, '', audit, '', '', lanid, '', start, end);
            return false;
        }

        var proname = $("#proname").val();
        var code = $("#code").val();
        var bname = $("#bname").val();
        if ($("#bcate_id").val()) {
            var platform = $("#bplatform").val();
        } else {
            var platform = $("#platform").val();
        }

        if (proname == '' && audit == '' && code == '' && platform == '' && lanid == '' && bname == '' && start == '' && end == '') {
            layer.msg("请选择需要导出产品或导出产品的条件");
            return false;
        }
        export_show('', proname, audit, code, platform, lanid, bname, start, end);
    }

    function export_show(guids, proname, audit, code, platform, lanid, bname, start, end) {
        if (guids.length != 0) {
            $("#guid").val(guids.join(','));
            var url = "exportExcel.html?lanid=" + lanid + "&start=" + start + "&end=" + end;
        } else {
            $("#guid").val();
            var url = "exportExcel.html?proname=" + proname + "&code=" + code + "&platform=" + platform + "&lanid=" + lanid + "&audit=" + audit + "&bname=" + bname + "&start=" + start + "&end=" + end;
        }
        layer.open({
            type: 2,
            area: ['70%', '90%'],
            fix: false, //不固定
            maxmin: true,
            shadeClose: false,//是否点击遮罩关闭弹窗
            shade: 0.2,
            resize: true,
            title: '选择导出Excel类型',
            btn: ['提交'],
            content: url + "&source=2",
            yes: function (index, obj) {
                layer.getChildFrame('#btn_save', index).click();
            }
        });
    }

    //重复检测
    function repeat(guid, lang) {
        $.post("{:url('admin/product/detectionRepeate')}", {
            lang: lang,
            guid: guid
        }, function (data) {
            if (data.code == 1) {
                layer.msg(data.msg, {icon: 1, time: 1000}, function () {
                    location.reload();
                });
            } else {
                layer.alert(data.msg, {icon: 2});
            }
        });
    }

    //批量改价
    function changePrice() {
        var start = $("#start").val();
        var end = $("#end").val();
        var lanid = $("#lang").val();
        var audit = $("#audit").val();
        var data = tableCheck.getData();
        if (data.length != 0) {
            change_show(data, '', audit, '', '', lanid, '', start, end);
            return false;
        }
        var proname = $("#proname").val();
        var code = $("#code").val();
        var bname = $("#bname").val();
        if ($("#bcate_id").val()) {
            var platform = $("#bplatform").val();
        } else {
            var platform = $("#platform").val();
        }
        if (proname == '' && audit == '' && code == '' && platform == '' && lanid == '' && bname == '' && start == '' && end == '') {
            layer.msg("请选择需要改价产品或需要改价产品的条件");
            return false;
        }
        change_show('', proname, audit, code, platform, lanid, bname, start, end);
    }
    function change_show(guids, proname, audit, code, platform, lanid, bname, start, end) {
        if (guids.length != 0) {
            $("#guid").val(guids.join(','));
            var url = "changePrice.html?lanid=" + lanid + "&start=" + start + "&end=" + end;
        } else {
            $("#guid").val();
            var url = "changePrice.html?proname=" + proname + "&code=" + code + "&platform=" + platform + "&lanid=" + lanid + "&audit=" + audit + "&bname=" + bname + "&start=" + start + "&end=" + end;
        }
        layer.open({
            type: 2,
            area: ['70%', '90%'],
            fix: false, //不固定
            maxmin: true,
            shadeClose: false,//是否点击遮罩关闭弹窗
            shade: 0.2,
            resize: true,
            title: '选择改价方式',
            btn: ['提交'],
            content: url + "&source=2",
            yes: function (index, obj) {
                layer.getChildFrame('#btn_save', index).click();
            }
        });
    }

    //价格同步
    function synchPrice() {
        var start = $("#start").val();
        var end = $("#end").val();
        var lanid = $("#lang").val();
        var audit = $("#audit").val();
        var data = tableCheck.getData();
        if (data.length != 0) {
            synch_show(data, '', audit, '', '', lanid, '', start, end);
            return false;
        }
        var proname = $("#proname").val();
        var code = $("#code").val();
        var bname = $("#bname").val();
        if ($("#bcate_id").val()) {
            var platform = $("#bplatform").val();
        } else {
            var platform = $("#platform").val();
        }
        if (proname == '' && audit == '' && code == '' && platform == '' && lanid == '' && bname == '' && start == '' && end == '') {
            layer.msg("请选择需要同步变体价格产品或需要同步变体价格产品的条件");
            return false;
        }
        synch_show('', proname, audit, code, platform, lanid, bname, start, end);
    }
    function synch_show(guids, proname, audit, code, platform, lanid, bname, start, end) {
        if (guids.length != 0) {
            $("#guid").val(guids.join(','));
            var url = "synchPrice.html?lanid=" + lanid + "&start=" + start + "&end=" + end;
        } else {
            $("#guid").val();
            var url = "synchPrice.html?proname=" + proname + "&code=" + code + "&platform=" + platform + "&lanid=" + lanid + "&audit=" + audit + "&bname=" + bname + "&start=" + start + "&end=" + end;
        }
        layer.open({
            type: 2,
            area: ['450px', '370px'],
            fix: false, //不固定
            maxmin: true,
            shadeClose: false,//是否点击遮罩关闭弹窗
            shade: 0.2,
            resize: true,
            title: '选择同步的数据',
            btn: ['提交'],
            content: url + "&source=2",
            yes: function (index, obj) {
                layer.getChildFrame('#btn_save', index).click();
            }
        });
    }
    //一键翻译
    function translateAll(lang) {
        var data = tableCheck.getData();
        if (data.length == 0) {
            layer.msg("请选择需要翻译的产品");
            return false;
        }
        window.external.Translate(lang, data.join(','))
    }
</script>
